<template>
    <div class="main-content">
        <echartsLayout :formState='formState' :dataListState='dataListState' :eCharts1State='eCharts1State' :eCharts2State='eCharts2State' 
        :eCharts3State='eCharts3State' :chartsListState='chartsListState' :tableState='tableState'>
            <div slot="form">
                <el-form :model="query" ref="query" label-width="45px" label-position="right">
                    <el-row :gutter="41">
                        <el-col :span="7">
                            <el-form-item label="省份" prop="provinceId">
                                <province v-model="query.provinceId" :name.sync="query.provinceName"></province>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="市区" prop="cityId">
                                <city v-model="query.cityId" :province="query.provinceId" :name.sync="query.cityName"></city>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item label="区县" prop="areaId">
                                <region v-model="query.areaId" :city="query.cityId" :name.sync="query.areaName"></region>
                            </el-form-item>
                        </el-col>
                    </el-row> 
                    <el-row :gutter="41">
                        <el-col :span="7">
                            <el-form-item label="月份">
                                 <el-date-picker
                                    v-model="query.startDate" 
                                    :clearable="false" 
                                    :picker-options="query.pickerOptions1" 
                                    :editable="false"  style="width:100%"
                                    type="month"
                                    placeholder="选择月份">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="3" :offset="14">
                            <div class="top-line">
                                <el-button type="primary" class="btn-common" @click="onSubmit">查询</el-button>
                            </div>
                        </el-col>
                    </el-row> 
                </el-form>
            </div> 
            <div slot="dataList" v-loading='dataLoading'>
                <el-row :gutter="20">
                    <el-col :span="8" class="data-item">
                        <div class="item">
                            <div class="title">现金消费额</div>
                            <div class="data">{{staticData.cash?Number(staticData.cash).toFixed(2):'--'}}</div> 
                        </div>
                    </el-col>
                    <el-col :span="8" class="data-item">
                        <div class="item">
                            <div class="title">现金消费人数</div>
                            <div class="data">{{staticData.num?staticData.num:'--'}}</div> 
                        </div>
                    </el-col> 
                    <el-col :span="8" class="data-item">
                        <div class="item">
                            <div class="title">人均消费额</div>
                            <div class="data">{{staticData.averageCash?Number(staticData.averageCash).toFixed(2):'--'}}</div> 
                        </div>
                    </el-col>
                </el-row>
            </div>
            <div slot="table" >
                <div class="table-title">
                    <exportExcel @ee="exportFile"></exportExcel>
                </div> 
                <el-table :data="AnnounceList" border v-loading="loading" style="width: 100%" 
                :default-sort = "{prop: 'cash', order: 'descending'}" 
                @sort-change='sortChange'>
                    <el-table-column label="现金消费额" align="center" sortable='custom' prop="cash">
                        <template scope="scope">
                            <span>{{scope.row.cash | isNull}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="现金消费人数" align="center" sortable='custom' prop="num">
                        <template scope="scope">
                            <span>{{scope.row.num | isNull}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="人均消费额" align="center" sortable='custom' prop="averageCash">
                        <template scope="scope">
                            <span>{{scope.row.averageCash| isNull}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="区域" align="center">
                        <template scope="scope">
                            <span>{{scope.row.areaName | isNull}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="城市" align="center">
                        <template scope="scope">
                            <span>{{scope.row.cityName | isNull}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="省份" align="center">
                        <template scope="scope">
                            <span>{{scope.row.provinceName | isNull}}</span>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination-container" style="text-align:right">
                    <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="listQuery.total" :current-page.sync="listQuery.page" :page-sizes="config.pageSizes" 
                    :page-size="config.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>
        </echartsLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import province from 'components/CascadeRegion/province';
    import city from 'components/CascadeRegion/city';
    import region from 'components/CascadeRegion/region';
    import echartsLayout from 'components/TableLayout/echartsLayout'; 
    import { mapState, mapActions } from 'vuex';
    import { queryCash } from 'api/pingtaifenxi';
    import exportExcel from 'components/ExportExcel/index'; 
    export default{
        components: {echartsLayout, province, city, region, exportExcel},
        data() {
            return {
                dataLoading: false,
                loading: false,
                formState: false,
                listQuery: {// 分页
                    page: 1,
                    total: 0
                },
                dataListState: false,
                eCharts1State: true,
                eCharts2State: true,
                eCharts3State: true,
                chartsListState: true,
                tableState: false,
                query: {
                    provinceId: '',
                    cityId: '',
                    areaId: '',
                    provinceName: '',
                    cityName: '',
                    areaName: '',
                    startDate: moment(new Date()).format('YYYY-MM'),
                    pickerOptions1: {
                        disabledDate(time) {
                            return (time.getTime()) > (Date.now());
                        }
                    }
                },
                staticData: {
                    cash: '',
                    num: '',
                    averageCash: ''
                },
                AnnounceList: [],
                cashStatus: 1,
                numStatus: '',
                averageCashStatus: '',
                filterList: null
            }
        },
        computed: {
            ...mapState({
                config: state => state.config.table
            })
        },
        created() {
            this.searchFetchData();
        },
        methods: {
            ...mapActions(['ChangePageSize']),
           
            onSubmit() {
                this.listQuery.page = 1;
                this.searchFetchData();
            },

            // 列表
            searchFetchData() {
                this.loading = true;
                this.dataLoading= true;
                let dateYear = this.query.startDate==''?'':Number(moment(this.query.startDate).format('YYYY')); 
                let dateMonth = this.query.startDate==''?'':Number(moment(this.query.startDate).format('MM')); 
                let curPage = this.listQuery.page;
                let pageSize = this.config.pageSize;
                let provinceId = this.query.provinceId;
                let cityId = this.query.cityId;
                let areaId = this.query.areaId;
                let cashStatus = this.cashStatus;// 金额排序 0.升序 1.降序”
                let numStatus = this.numStatus;// 人数排序 0.升序 1.降序”, （
                let averageCashStatus = this.averageCashStatus;// 平均金额排序 
                queryCash(curPage, pageSize, provinceId, cityId, areaId, 
                    dateYear, dateMonth, cashStatus, numStatus, averageCashStatus).then(res => {
                        this.loading = false;
                        this.dataLoading= false;
                        this.staticData.cash=res.data.content.info.cash;
                        this.staticData.num=res.data.content.info.num;
                        this.staticData.averageCash=res.data.content.info.averageCash;
                        this.AnnounceList = res.data.content.list.list;
                        this.listQuery.total = res.data.content.list.pagination?res.data.content.list.pagination.total:0;
                    }).catch(() => {
                        this.loading = false;
                        this.dataLoading= false;
                    });
            },
            exportFile() {
                let provinceId = this.query.provinceId;
                let cityId = this.query.cityId;
                let areaId = this.query.areaId;
                let dateYear = this.query.startDate==''?'':moment(this.query.startDate).format('YYYY'); 
                let dateMonth = this.query.startDate==''?'':Number(moment(this.query.startDate).format('MM')); 
                let cashStatus = this.cashStatus;// 金额排序 0.升序 1.降序”
                let numStatus = this.numStatus;// 人数排序 0.升序 1.降序”, （
                let averageCashStatus = this.averageCashStatus;// 平均金额排序 
                window.location.href = `${LOCAL_API.scy}/report/outExcelByPayInfo?provinceId=${provinceId}&cityId=${cityId}&areaId=${areaId}&dateYear=${dateYear}&dateMonth=${dateMonth}&cashStatus=${cashStatus}&numStatus=${numStatus}&averageCashStatus=${averageCashStatus}`
            },
            sortChange({ column, prop, order }) {
                this.cashStatus=prop=='cash'?order=='ascending'?0:1:'';
                this.numStatus=prop=='num'?order=='ascending'?0:1:'';
                this.averageCashStatus=prop=='averageCash'?order=='ascending'?0:1:'';
                this.searchFetchData();
            },
            handleSizeChange(val) {
                this.listQuery.page = 1;
                this.ChangePageSize(val);
                this.searchFetchData();
            },
            handleCurrentChange(val) {
                this.listQuery.page = val;
                this.searchFetchData();
            }
        }
    }
</script>
<style lang="scss" scoped>
    .main-content {
        position: relative;
        $bgcolor: #fff;
    }
</style>                     